<?php
namespace app\admin\controller;

use app\common\controller\Backend;

use think\Request;

class Index extends Backend
{
	// 不需要登录的页面
	protected $NoNeedLogin = ['login'];
	
    public function index()
    {
		
        return $this->fetch();
		
	}
	
	// 后台登录
	public function login()
	{
	
	    $LoginAdmin = !empty(cookie('LoginAdmin')) ? cookie('LoginAdmin') : '';
	    
		if($LoginAdmin)
		{
			$adminList = model('Admin')->select();

			$adminid = 0;

			foreach($adminList as $item)
			{
				$val = md5($item['id'].$item['salt']);

				if($LoginAdmin == $val)
				{
					$adminid = $item['id'];
					break;
				}
			}

			$admin = model('Admin')->find($adminid);

			if(!$admin)
			{
				cookie('LoginAdmin',null);
				$this->error('非法登录',url('admin/index/login'));
			}

			if($admin['state'] != 1)
			{
				cookie('LoginAdmin',null);
				$this->error('管理员已禁用');
			}

			$this->success('您已登录了，无须重复登录',url('admin/index/index'));

		}


		if($this->request->isPost())
		{
			$params = $this->request->param();

			if(!captcha_check($params['captcha']))
			{
				$this->error('验证码错误');
			}

			$admin = model('Admin')->where('username',$params['username'])->find();

			if(!$admin)
			{
				$this->error('管理员不存在');
			}

			$password = md5($params['password'].$admin['salt']);

			if($password != $admin['password'])
			{
				$this->error('密码错误');
			}

			// 判断管理员的状态
			if($admin['state'] != 1)
			{
				cookie('LoginAdmin',null);
				$this->error('管理员已禁用');
			}

			if(!empty($params['rememberme']) && $params['rememberme'] == 1)
			{
				cookie('LoginAdmin',md5($admin['id'].$admin['salt']),5 * 24 * 3600 + time());
			}else{
				cookie('LoginAdmin',md5($admin['id'].$admin['salt']));
			}

			$this->success('登录成功',url('admin/index/index'));

		}	
		
		// 动态使用模板布局
		$this->view->engine->layout(false);
	    return $this->fetch();
	}
	
	// 注册页面
	public function register(){
		//
		
		// 获取表单请求过来的数据
		if($this->request->isPost()){
			//get post
			$params = $this->request->param();
			// halt($params);
			// 验证码验证
			if(!captcha_check($params['vercode'])){
				// 跳转中间页
				$this->error('验证码错误');
			}
			// 判断二次密码是否一致
			if($params['pass'] != $params['repass']){
				$this->error('密码与确认密码不一致');
			}
			// 生成密码盐
			$salt = build_ranstr();
			
			// 进行加密
			$password = md5($params['pass'].$salt);
			
			$username = model('Admin')->where('username',$params['username'])->find();
			
			if($username){
				$this->error('该管理员已经存在');
			}
			 
			 // 封装插入数据库的数组
			 $data = [
			    'username' => $params['username'],
			    'nickname' => $params['nickname'],
			    'password' => $password,
			    'salt' => $salt,
			    'state' => 1,
			];
			
			// 验证
			$result = model('Admin')->save($data);
			
			if($result === FALSE){
				$this->error(model('Admin')->getError());
			}else{
				$this->success('添加成功',url('admin/index/adminlist'),null,0);
			}
		}
		// 输出视图
		// 动态使用模板布局
		// $this->view->engine->layout(false);
		return $this->fetch();
	}
	
	// 管理员列表
	public function adminlist(){
		// 
		// $adminlist = model('Admin')->select();
		
		// 
		$adminlist = model('Admin')->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			'adminlist' => $adminlist
		]);
	}
	
	// 编辑
	public function upadmin(){ 
		
		// id
		$id = $this->request->param('id');
		
		$adminup = model('Admin')->where('id',$id)->find();
		
		if($this->request->isPost())
		{
			//
			$params = $this->request->param();
			
			$action = $this->request->param('password');
			
			// 验证码验证
			if(!captcha_check($params['vercode'])){
				// 跳转中间页
				$this->error('验证码错误');
			}
			
			
			if($action)
			{
				// 
				$params = $this->request->param();
				// 管理员昵称
				$nickname = $this->request->param('nickname');
				// 生成新密码盐
				$salt = build_ranstr();
				
				$list = model('Admin')->where('id',$id)->find();
				
				// 旧用户密码
				$oldpassword = $list['password'];
				// 旧密码盐
				$oldsalt = $list['salt'];
				
				// 用户输入的 旧密码
				$password = md5($params['password'].$oldsalt);
				
				// 新密码
				$newpassword = $params['newpassword'];
				
				// // 确认密码
				$pass = $params['repass'];
				
				// 状态
				$state = $params['state'];
				
				// 
				if($password != $oldpassword){
					$this->error('旧密码输入错误',url('home/user/profile'),null,0);
				}
				
				if($newpassword != $pass){
					$this->error('确认密码不一致',url('home/user/profile'),null,0);
				}
				
				
				// 封装数据
				$data = [
					'id' => $id,
					'nickname' => $nickname,
					'password' => md5($newpassword.$salt),
					'salt' => $salt,
					'state' => $state
				];
		
				// isUpdate == true ?  save = 更新数据  : 插入
				$result = model('Admin')->isUpdate(true)->save($data);
		
				if($result === FALSE)
				{
					$this->error('编辑失败');
				}else{
					// cookie('LoginUser',null);
					$this->success('编辑成功',url('admin/index/adminlist'),null,0);
				}
			}else{
				
				//
				$params = $this->request->param();
				
				// 管理员昵称
				$nickname = $this->request->param('nickname');
				
				// 状态
				$state = $params['state'];
				
				// 封装数据
				$data = [
					'id' => $id,
					'nickname' => $nickname,
					'state' => $state
				];
				
				// isUpdate == true ?  save = 更新数据  : 插入
				$result = model('Admin')->isUpdate(true)->save($data);
						
				if($result === FALSE)
				{
					$this->error('编辑失败');
				}else{
					// cookie('LoginUser',null);
					$this->success('编辑成功',url('admin/index/adminlist'),null,0);
				}
				
			}
		}
		// 动态使用模板布局
		// $this->view->engine->layout(false);
		return $this->fetch('',[
			
			'adminup' => $adminup
			
		]);
			
	}
	
	// 删除单条
	public function deladmin(){
		
		if($this->request->isAjax()){
			
			// 获取参数
			$id = $this->request->param('id');
			
			$result = model('Admin')->find($id);
			
			if(!$result){
				$this->error('没有该管理员');
			}
				
			$result = model('Admin')->where('id',$id)->delete();
			
			if($result){
				
				$this->success('删除成功',url('admin/index/adminlist'),null,0);
				
			}else{
				
				$this->error('删除失败');
			}
		}
		
	}
	
	// 删除
	public function del(){
		
		if($this->request->isAjax()){
			
			// 获取参数
			$action = $this->request->param('action');
			
			// 获取删除数据
			$attstr = $this->request->param('attstr');
			
			if($action){
				
				$result = model('Admin')->delete();
				
				if($result){
					
					$this->success('删除成功',url('admin/index/adminlist'),null,0);
					
				}else{
					
					$this->error('删除失败');
				}
				
			}
			
			if(!$attstr){
				
				$this->error('暂无数据可删');
				
			}
			
			$result = model('Admin')->where('id',$attstr)->delete($attstr);
			
			if($result){
				
				$this->success('删除成功');
				
			}else{
				
				$this->error('删除失败');
			}
			
		}
		
	}
	
	// 启用
	public function qidong(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('Admin')->where('id', $item)->update(['state' => '1']);
				
			}
			
			if($result === FALSE)
			{
				$this->error('启用失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('启用成功',url('admin/index/adminlist'),null,0);
			}
			
		}
		
	}
	
	// 禁用
	public function jinyong(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('Admin')->where('id', $item)->update(['state' => '0']);
				
			}
			
			if($result === FALSE)
			{
				$this->error('禁用失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('禁用成功',url('admin/index/adminlist'),null,0);
			}
			
		}
		
	}
	
}